//
//  FirstViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "FirstViewController.h"
#import "TitleCollectionViewCell.h"
#import "FirstCollectionViewCell.h"
#import "Model.h"
#import "Fit.h"


@interface FirstViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *titleCollectionView;
@property(nonatomic, retain)UICollectionView *listCollectionView;
@property(nonatomic, retain)NSArray *urlArray;
@property(nonatomic, retain)NSArray *titleArray;
@property(nonatomic, assign)NSInteger flag;

@end

@implementation FirstViewController
-(void)dealloc
{
    [_titleCollectionView release];
    [_listCollectionView release];
    [_urlArray release];
    [_titleArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
     [_listCollectionView reloadData];
    [self changeColor];
}

-(void)changeColor{
    self.titleCollectionView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [self.titleCollectionView reloadData];
    [self.listCollectionView reloadData];
}


-(void)loadView
{
    [super loadView];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    UICollectionViewFlowLayout *titleLayout = [[UICollectionViewFlowLayout alloc]init];
    titleLayout.itemSize = CGSizeMake(70 * W, 30 * H);
    titleLayout.sectionInset = UIEdgeInsetsMake(5 * W, 20 * H, 0, 0);
    titleLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.titleCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * H) collectionViewLayout:titleLayout];
    _titleCollectionView.dataSource = self;
    _titleCollectionView.delegate = self;
    _titleCollectionView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_titleCollectionView];
    [_titleCollectionView registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_titleCollectionView release];
    
    UICollectionViewFlowLayout *listLayout = [[UICollectionViewFlowLayout alloc]init];
    listLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 113 - 40 * H);
    listLayout.minimumLineSpacing = 0;
    listLayout.minimumInteritemSpacing = 0;
    listLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    listLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.listCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40 * W, self.view.frame.size.width, self.view.frame.size.height - 153 * H) collectionViewLayout:listLayout];
    _listCollectionView.dataSource = self;
    _listCollectionView.delegate = self;
    _listCollectionView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    _listCollectionView.pagingEnabled = YES;
    [self.view addSubview:_listCollectionView];
    [_listCollectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"identifier2"];
    [_listCollectionView release];
    
    
    NSString *allString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/all/?sort=default&start=0&latitude=38.883664&longitude=121.544551&sign=167ec2032789c6c921d8c0ab70ddb4d9", _num, _IDString];
    
    NSString *sightsString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/sights/?sort=default&start=0&latitude=38.883691&longitude=121.544559&sign=399aadff87d4edc96ace31c98280e570", _num, _IDString];
    
    NSString *hotelString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/hotel/?sort=default&start=0&latitude=38.883730&longitude=121.544610&sign=3c5b4df1a714c16aefdcb31a65970c17", _num, _IDString];
    
    NSString *restaurantString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/restaurant/?sort=default&start=0&latitude=38.883736&longitude=121.544494&sign=92ad3c0fd819e225e045424b666383e9",  _num, _IDString];
    
    NSString *experienceString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/experience/?sort=default&start=0&latitude=38.883641&longitude=121.544671&sign=f596bbc1564374fb8eb05ae7584c335f", _num, _IDString];
    
    NSString *mallString = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/mall/?sort=default&start=0&latitude=38.883592&longitude=121.544641&sign=d000569824be142640be256aa4f64062",  _num, _IDString];
    
    self.urlArray = [NSArray arrayWithObjects:allString, sightsString, hotelString, restaurantString, experienceString, mallString, nil];
    self.titleArray = @[@"全部", @"景点", @"住宿", @"餐厅", @"休闲娱乐", @"购物"];
    
    
}

#pragma mark collectionView cell的样式,个数
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        TitleCollectionViewCell *titleCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
        titleCell.backgroundColor =  [Model shareInstance].BackgroundColor;
        titleCell.label.text = _titleArray[indexPath.row];
        if (_flag == indexPath.row) {
            titleCell.label.backgroundColor = [UIColor colorWithRed:0.7 green:0.34 blue:0.65 alpha:1];
        }
        else {
            titleCell.label.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
        }
        return titleCell;
    }else
    {
        FirstCollectionViewCell *listCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier2" forIndexPath:indexPath];
        if (_urlArray.count != 0) {
            listCell.urlString = _urlArray[indexPath.row];
        }
        listCell.backgroundColor = [Model shareInstance].BackgroundColor;
        return listCell;
    }
}
#pragma mark collectionView cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
#pragma mark collectionView 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        _flag = indexPath.row;
        _listCollectionView.contentOffset = CGPointMake(_flag * 375 * W, 0);
        [_listCollectionView reloadData];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _listCollectionView) {
        _titleCollectionView.contentOffset = CGPointMake(_listCollectionView.contentOffset.x / self.view.frame.size.width * 65 * W, 0);
        _flag = _listCollectionView.contentOffset.x / self.view.frame.size.width;
        [_titleCollectionView reloadData];
    }
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
